package com.centon.auto_code.project.service.impl;

import com.centon.auto_code.comm.ServiceCode;
import com.centon.auto_code.domain.ProDatabase;
import com.centon.auto_code.domain.ProDatabase;
import com.centon.auto_code.domain.ProTable;
import com.centon.auto_code.domain.gen.ProDatabaseCriteria;
import com.centon.auto_code.domain.gen.ProTableCriteria;
import com.centon.auto_code.mapper.ProDatabaseMapper;
import com.centon.auto_code.mapper.ProTableMapper;
import com.centon.auto_code.project.service.DatabaseService;
import com.centon.auto_code.project.vo.ProDatabaseVo;
import com.centon.auto_code.project.vo.ProDatabaseVo;
import com.centon.auto_code.project.vo.ProTableVo;
import com.centon.auto_code.util.BeanUtil;
import com.centon.auto_code.util.CommUtils;
import com.centon.auto_code.vo.BaseRes;
import com.centon.auto_code.vo.PageData;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.rmi.CORBA.Util;
import java.util.Date;
import java.util.List;

@Service
public class DatabaseServiceImpl implements DatabaseService {

    @Resource
    private ProDatabaseMapper proDatabaseMapper;

    @Resource
    private ProTableMapper proTableMapper;

    @Override
    public BaseRes<PageData<ProDatabaseVo>> entityPage(ProDatabaseVo proDatabaseVo) {

        BaseRes<PageData<ProDatabaseVo>> res = new BaseRes<>();

        res.setData(new PageData<>());
        res.getData().setCurrent(proDatabaseVo.getCurrent());
        res.getData().setPageSize(proDatabaseVo.getPageSize());

        ProDatabaseCriteria example = new ProDatabaseCriteria();
        int mysqlLength = res.getData().getPageSize();
        int mysqlOffset = (res.getData().getCurrent() - 1) * mysqlLength;

        example.setMysqlLength(mysqlLength);
        example.setMysqlOffset(mysqlOffset);

        ProDatabaseCriteria.Criteria mCriteria = example.createCriteria();
        mCriteria.andDelFlagEqualTo(0);

        long totalCount = proDatabaseMapper.countByExample(example);
        res.getData().setTotal(totalCount);
        if (totalCount > 0) {
            List<ProDatabase> proDatabaseList = proDatabaseMapper.selectByExample(example);
            List<ProDatabaseVo> rows = BeanUtil.voConvertList(proDatabaseList, ProDatabaseVo.class);
            res.getData().setRows(rows);
        }
        return res;
    }

    @Override
    public BaseRes<List<ProTableVo>> tableList(String dbId) {
        ProTableCriteria mProTableCriteria = new ProTableCriteria();
        mProTableCriteria.createCriteria().andDatabaseIdEqualTo(dbId);
        List<ProTable> tables = proTableMapper.selectByExample(mProTableCriteria);

        List<ProTableVo> list = BeanUtil.voConvertListHasIn(tables, ProTableVo.class, "id", "name", "comment");
        return new BaseRes<>(list, ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> addDataBase(ProDatabaseVo proDatabaseVo) {
        System.out.println("id="+CommUtils.randomUUID());
        proDatabaseVo.setId(CommUtils.randomUUID());
        proDatabaseVo.setCreateTime(new Date());
        proDatabaseMapper.insertSelective(proDatabaseVo);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<ProDatabaseVo> dataBaseDetail(ProDatabaseVo proDatabaseVo) {

        BaseRes<ProDatabaseVo> res = new BaseRes<>();
        ProDatabase proDatabase = proDatabaseMapper.selectByPrimaryKey(proDatabaseVo.getId());

        if (proDatabase!= null){
            ProDatabaseVo databaseVo = BeanUtil.voConvert(proDatabase, ProDatabaseVo.class);
            res.setData(databaseVo);
        }
        res.setServiceCode(ServiceCode.SUCCESS);

        return res;
    }
}
